library(tidyverse)

###read in coded data
# collected data from multiple journals using skin color measure
coded_data<-read.csv("articles_skin_color.csv")

coded_data<-coded_data %>%
  mutate(method_name = case_when(
    method1 == 0 ~"No skin color measure",
    method1 == 1 ~ "Self-Report: Likert Scale",
    method1 == 2 ~ "Self-Report: Palette Scale",
    method1 == 3 ~ "Interviewer-Report: Palette Scale/Other",
    method1 == 4 ~ "Spectrometer Measure",
    method1 == 5 ~ "Photo Elicitation: Asked to classify based on images",
    method1 == 6 ~ "Qualitative Interviews: Respondent identifies skin color based on conversation"
  ))

## keep only ones that have a skin color measure
skin_color_measure_only<-subset(coded_data, method1 != 0)
skin_color_measure_only<-skin_color_measure_only %>%
  mutate(simple_measure = case_when(
    method1 == 1 ~ 1,
    method1 == 2 ~ 1,
    method1 == 3 ~1,
    method1 == 4 ~ 4,
    method1 == 5 ~ 5,
    method1 == 6 ~ 6
    
  ))

skin_color_measure_only<-skin_color_measure_only %>%
  mutate(method_name = case_when(
    # simple_measure == 0 ~"No skin color measure",
    simple_measure == 1 ~ "Likert/Color Palette Scale",
    simple_measure == 2 ~ "Self-Report: Palette Scale",
    simple_measure == 3 ~ "Interviewer-Report: Palette Scale/Other",
    simple_measure == 4 ~ "Spectrometer Measure",
    simple_measure == 5 ~ "Photo Elicitation: Asked to classify based on images",
    simple_measure == 6 ~ "Qualitative Interviews: Respondent identifies skin color based on conversation"
  ))


#make summaries
summary_skin_color<- skin_color_measure_only %>%
  group_by( method_name) %>%
  summarise( 
    n = n(),
    total = sum(n)) %>%
  mutate(percent = round((n/sum(n)), digits = 2))

#make summaries by year
summary_skin_color_year <- skin_color_measure_only %>%
  group_by(Publication.Year, method_name) %>%
  summarise( 
    n = n(),
    total = sum(n)) %>%
  mutate(percent = round((n/sum(n)), digits = 2))

#Figure: Counts of Skin Color Measurement Methods by Publication Year
counts_method_year_plot <- ggplot(summary_skin_color_year, 
                                  aes(x = Publication.Year,
                                      y = n,
                                      fill = method_name)) +
  geom_bar(position = "dodge", 
           stat = "identity") + 
  theme_minimal()+
  xlab("")+ 
  ylab("Counts")+
  labs(title = "Counts of Skin Color Measurement Methods by Publication Year ",
       subtitle = "Published Articles in Social Sciences") +
  theme(plot.title = element_text(size=18, 
                                  face = "bold"), 
        plot.subtitle = element_text(size=12))+
  labs(fill = "Skin Color Measure Type")


#Figure: "Percent of Skin Color Measurement Methods" ####
pct_color_methods <- ggplot(summary_skin_color,
         aes(x = reorder(method_name, 
                         -percent),
             y = percent,
             fill = method_name
         )) + 
  geom_bar(stat = "identity") +
  theme_minimal() + 
  xlab("") + 
  ylab("Percent") + 
  labs(title = "Percent of Skin Color Measurement Methods ", 
       subtitle = "Published Articles in Social Sciences") + 
  theme(plot.title = element_text(size = 18, 
                                  face = "bold"),
        axis.text.x = element_blank(),
        plot.subtitle = element_text(size = 12)) +
  labs(fill = "Skin Color Measure Type") + 
  geom_text(aes(label = percent), 
            vjust = -0.25)

# Save plots
plot_height <- 5
plot_width <- 9
ggsave("counts_method_year_plot.pdf",
       counts_method_year_plot,
       height = plot_height,
       width = plot_width,
       units = "in")
ggsave("pct_color_methods.pdf",
       pct_color_methods, 
       height = plot_height,
       width = plot_width,
       units = "in")






